var app = getApp()
var Utils = require('../../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tablist: ['全部', "待支付", "待接诊", "已到诊", "已结束", "退款单"],
    currentData: 0,
    list: [],
    page: 0,
    pagesize: 10,
    dingdanall: [],
    currentData: 0
  },
  //点击切换，滑块index赋值
  checkCurrent: function (e) {
    if (this.data.currentData === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentData: e.target.dataset.current
      })
    }
    this.setData({
      list: [],
      page: 0,
      currentData: e.target.dataset.current
    })
    this.getList(this.data.currentData);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var keyword = options.keyword;

    var that = this;
    that.setData({
      keyword: keyword,
    })
  },
  getList(index) {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/green.order_list",
      data: {
        openid: wx.getStorageSync('openid'),
        page: that.data.page,
        pagesize: that.data.pagesize,
        type: index,
        keyword: that.data.keyword
      },
      success: function (res) {
        console.log(res)
        var page = that.data.page + 1;
        that.setData({
          list: that.data.list.concat(res.data),
          page: page,
        })
      }
    })
  },
  //取消订单
  cancel(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    app.util.request({
      'url': "entry/wxapp/green.order_change",
      data: {
        id: id,
        ifpay: '8'
      },
      success: function (res) {
        that.setData({
          page: 0,
          list: []
        })
        that.getList(that.data.currentData)
      }
    })
  },
  pingjia(e) {
    console.log(e)
    var keywords = e.currentTarget.dataset.keywords
    var zid = e.currentTarget.dataset.zid
    var did = e.currentTarget.dataset.did;
    var orders = e.currentTarget.dataset.orders
    var j_id = e.currentTarget.dataset.j_id
    wx.navigateTo({
      url: '/hyb_yl/lvtongserver/pages/pingjia/pingjia?keywords=' + keywords + '&orders=' + orders + '&j_id=' + j_id + '&did=' + did + '&zid=' + zid,
    })
  },
  //去支付
  gopay(e) {
    var that = this
    var back_orser = e.currentTarget.dataset.back_orser
    var money = e.currentTarget.dataset.money
    var key_words = e.currentTarget.dataset.key_words
    var zid = e.currentTarget.dataset.zid
    var did = e.currentTarget.dataset.did;
    var j_id = e.currentTarget.dataset.j_id;
    app.util.request({
      'url': 'entry/wxapp/green.payorder',
      header: {
        "Content-Type": "application/xml"
      },
      method: "GET",
      data: {
        openid: wx.getStorageSync('openid'),
        z_tw_money: money,
        orders: back_orser,
        key_words: key_words
      },
      success: function (res) {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr, //字符串随机数
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          success: function (res) {

          },
          fail: function (res) {

          },
          'complete': function (res) {
            if (res.errMsg == 'requestPayment:ok') {
              wx.showToast({
                title: '预约成功！',
                icon: 'none',
                success: function () {
                  setTimeout(function () {
                    that.setData({
                      list: [],
                      page: 0
                    })
                    that.getList(that.data.currentData);
                  }, 1900)
                }
              })
              return;
            }
          }
        })
      }
    })

  },
  //去退款
  refund(e) {
    var that = this
    var id = e.currentTarget.dataset.id;
    app.util.request({
      'url': "entry/wxapp/green.order_change",
      data: {
        id: id,
        ifpay: '5'
      },
      success: function (res) {
        that.setData({
          list: [],
          page: 0
        })
        that.getList(that.data.currentData);
      }
    })
  },
  //完成
  finish() {},
  //删除
  deleteitem(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var back_orser = e.currentTarget.dataset.back_orser;
    app.util.request({
      'url': "entry/wxapp/green.del_order",
      data: {
        id: id,
        back_orser: back_orser
      },
      success: function (res) {
        that.setData({
          page: 0,
          list: []
        })
        that.getList(that.data.currentData)
      }
    })
  },
  //详情
  lookdetail: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this
    var orders = e.currentTarget.dataset.orders
    var j_id = e.currentTarget.dataset.j_id
    var key_words = e.currentTarget.dataset.key_words
    var zid = e.currentTarget.dataset.zid
    var ifpay = e.currentTarget.dataset.ifpay
    var money = e.currentTarget.dataset.money
    var id = e.currentTarget.dataset.id
    var currentData = that.data.currentData
    var did = e.currentTarget.dataset.did;
    wx.setStorage({
      key: 'userinfotype',
      data: 'user',
    })
    let userInfo = wx.getStorageSync('userInfo')
    wx.setStorage({
      key: 'userInfo',
      data: userInfo,
    })
    let thumb = e.currentTarget.dataset.thumb //医生头像
    wx.setStorage({
      key: 'thumb',
      data: thumb,
    })
    let sate = 0
    wx.setStorageSync('sate', sate) //证明是用户发送的信息

    if (ifpay == 5) {
      wx.showToast({
        title: '退款申请中无法查看详情',
        icon: 'none'
      })
      return false;
    }
    if (ifpay == 6) {
      wx.showToast({
        title: '已退款',
        icon: 'none'
      })
      return false;
    }

    wx.navigateTo({
      url: '/hyb_yl/krecovery/pages/postoperative_ordercont/postoperative_ordercont?txt=yes&zid=' + zid + '&back_orser=' + orders + '&key_words=' + key_words + '&j_id=' + j_id + '&ifpay=' + ifpay + '&money=' + money + '&currentData=' + currentData + '&id=' + id + '&did=' + did,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var currentData = that.data.currentData

    that.getList(currentData)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList(this.data.currentData)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})